<!DOCTYPE html>
<html>
<head>
    <title>Surface-Surface Intersection</title>

    <script src="js/verb.js"></script>
    <script src="js/three.min.js"></script>
    <script src="js/verbToThreeConversion.js"></script>
    <script src="js/OrbitControls.js"></script>
    <script src="js/codemirror.js"></script>
    <script src="js/javascript.js"></script>
    <script src="js/threeBasic.js"></script>

    <link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,300' rel='stylesheet' type='text/css'>

    <link rel="stylesheet" href="css/codemirror.css">
    <link rel="stylesheet" href="css/example.css">
    <link rel="stylesheet" href="css/colorforth.css">
</head>
<body>

<div id="button">Show/Hide Code</div>
<div id="viewer" class="col">
    <div id="title"></div>
</div>
<div id="code-container" class="col scroll-y"></div>

</body>
<script id="script">
	setupScene();
	function exNurbsSurfacePlane(){

		var degree = 3
			, knots = [0, 0, 0, 0, 0.333, 0.666, 1, 1, 1, 1]
			, pts = [ 	[ [0, 0, -10], 	[10, 0, 0], 	[20, 0, 0], 	[30, 0, 0] , 	[40, 0, 0], [50, 0, 9] ],
						[ [0, -10, 0], 	[10, -10, 10], 	[20, -10, 10], 	[30, -10, 0] , [40, -10, 0], [50, -10, 0]	],
						[ [0, -20, 0], 	[10, -20, 10], 	[20, -20, 10], 	[30, -20, 0] , [40, -20, -2], [50, -20, 0] 	],
						[ [0, -30, 0], 	[10, -30, 0], 	[20, -30, 0], 	[30, -30, 0] , [40, -30, 0], [50, -30, 0]     ],
						[ [0, -40, 0], 	[10, -40, 0], 	[20, -40, 0], 	[30, -40, 4] , [40, -40, -20], [50, -40, 0]     ],
						[ [0, -50, 12], [10, -50, 0], 	[20, -50, 0], 	[30, -50, 0] , [50, -50, 0], [50, -50, 15]     ],     ];
		srf1 = new verb.geom.NurbsSurface.byKnotsControlPointsWeights( degree, degree, knots, knots, pts );

		var p5 = [50,-50,3]
			, p6 = [50,0,3]
			, p7 = [0,0,3]
			, p8 = [0,-50,5];

		srf2 = verb.geom.NurbsSurface.byCorners( p5, p6, p7, p8 );

	}


    function exNurbsSurfaceCylindrical() {

        var degree = 3
            , knots = [0, 0, 0, 0, 0.333, 0.666, 1, 1, 1, 1]
            , pts = [[[0, 0, -10], [10, 0, 0], [20, 0, 0], [30, 0, 0], [40, 0, 0], [50, 0, 9]],
            [[0, -10, 0], [10, -10, 10], [20, -10, 10], [30, -10, 0], [40, -10, 0], [50, -10, 0]],
            [[0, -20, 0], [10, -20, 10], [20, -20, 10], [30, -20, 0], [40, -20, -2], [50, -20, 0]],
            [[0, -30, 0], [10, -30, 0], [20, -30, 0], [30, -30, 0], [40, -30, 0], [50, -30, 0]],
            [[0, -40, 0], [10, -40, 0], [20, -40, 0], [30, -40, 4], [40, -40, -20], [50, -40, 0]],
            [[0, -50, 12], [10, -50, 0], [20, -50, 0], [30, -50, 0], [50, -50, 0], [50, -50, 15]],];
        srf1 = new verb.geom.NurbsSurface.byKnotsControlPointsWeights(degree, degree, knots, knots, pts);

        var axis = [0, 0, 1]
            , xaxis = [1, 0, 0]
            , base = [0, 0, 0]
            , height = 50
            , radius = 30;

        srf2 = new verb.geom.CylindricalSurface(axis, xaxis, base, height, radius);


    }
	function exPlaneCylindricalSurface(){

		var axis = [0,0,1]
			, xaxis = [1,0,0]
			, base = [0,0,0]
			, height = 50
			, radius = 30;

		srf1 = new verb.geom.CylindricalSurface( axis, xaxis, base, height, radius );

		var p5 	= [50,50,50]
			, p6 = [-50,50,50]
			, p7 = [-50,-50,0]
			, p8 = [50,-50,0];

		srf2 = verb.geom.NurbsSurface.byCorners( p5, p6, p7, p8 );

	}

    function exTorusCylindricalSurface(){

		// center, xaxis, yaxis, radius
		var profile = new verb.geom.Circle( [5,0,0], [1,0,0], [0,0,1], 2 );

		var base = [0,0,0];
		var axis = [0,0,1];
		var angle = 2 * Math.PI
		srf1 = new verb.geom.RevolvedSurface( profile, base, axis, angle );

		var axis = [-1,0,0]
			, xaxis = [0,0,1]
			, base = [8,0,0]
			, height = 16
			, radius = 2;

		srf2 = new verb.geom.CylindricalSurface( axis, xaxis, base, height, radius );

	}

	function exTorusCylindricalSurface2(){

		// center, xaxis, yaxis, radius
		var profile = new verb.geom.Circle( [5,0,0], [1,0,0], [0,0,1], 2 );

		var base = [0,0,0];
		var axis = [0,0,1];
		var angle = 2 * Math.PI
		srf1 = new verb.geom.RevolvedSurface( profile, base, axis, angle );

		var axis = [-1,0,0]
			, xaxis = [0,0,1]
			, base = [8,0,1]
			, height = 16
			, radius = 2;

		srf2 = new verb.geom.CylindricalSurface( axis, xaxis, base, height, radius );

	}

	//exNurbsSurfacePlane();
	//exNurbsSurfaceCylindrical();
	//exPlaneCylindricalSurface();
	exTorusCylindricalSurface();
	//exTorusCylindricalSurface2();
    
    addMeshToScene( srf1.toThreeGeometry()  );
	addMeshToScene( srf2.toThreeGeometry()  );
    
    var res;
	var runs = 10;

	var d1 = Date.now();

	for (var i = 0 ; i < runs; i++){
		res = verb.geom.Intersect.surfaces( srf1, srf2, 1e-6 );
	}

	var d2 = Date.now();

	console.log( "intersected in ", (d2 - d1) / runs, " ms and " + res.length + " curves were discovered");

    for (var i = 0; i < res.length; i++){
		var res2 = res[i].toThreeGeometry();
		addCurveToScene( res2 );
	}

    renderScene();

</script>

<script src="js/example.js"></script>

</html>
